package com.itwxz.system.controller.pc;

import com.itwxz.common.annotation.Log;
import com.itwxz.common.core.controller.BaseController;
import com.itwxz.common.core.domain.AjaxResult;
import com.itwxz.common.core.page.TableDataInfo;
import com.itwxz.common.enums.BusinessType;
import com.itwxz.common.utils.poi.ExcelUtil;
import com.itwxz.system.domain.po.WxzOrderHistory;
import com.itwxz.system.domain.dto.SelectHistoryOrderDto;
import com.itwxz.system.service.IWxzOrderHistoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 历史订单Controller
 *
 * @author gastronome
 * @date 2025-04-06
 */
@Tag(name = "历史订单Controller")
@Slf4j
@RestController
@RequestMapping("/wxz/history")
public class WxzOrderHistoryController extends BaseController {
	@Autowired
	private IWxzOrderHistoryService orderHistoryService;

	/**
	 * 查询历史订单列表
	 */
	@Operation(summary = "查询历史订单列表")
	@PreAuthorize("@ss.hasPermi('wxz:history:list')")
	@GetMapping("/list")
	public TableDataInfo list(SelectHistoryOrderDto orderHistory) {
		startPage();
		List<SelectHistoryOrderDto> list = orderHistoryService.selectWxzOrderHistoryList(orderHistory);
		return getDataTable(list);
	}

	/**
	 * 导出历史订单列表
	 */
	@Operation(summary = "导出历史订单列表")
	@PreAuthorize("@ss.hasPermi('wxz:history:export')")
	@Log(title = "历史订单", businessType = BusinessType.EXPORT)
	@PostMapping("/export")
	public void export(HttpServletResponse response, SelectHistoryOrderDto orderHistory) {
		List<SelectHistoryOrderDto> list = orderHistoryService.selectWxzOrderHistoryList(orderHistory);
		ExcelUtil<SelectHistoryOrderDto> util = new ExcelUtil<SelectHistoryOrderDto>(SelectHistoryOrderDto.class);
		util.exportExcel(response, list, "历史订单数据");
	}

	/**
	 * 获取历史订单详细信息
	 */
	@Operation(summary = "获取历史订单详细信息")
	@PreAuthorize("@ss.hasPermi('wxz:history:query')")
	@GetMapping(value = "/{id}")
	public AjaxResult getInfo(@PathVariable("id") Long id) {
		return success(orderHistoryService.getById(id));
	}

	/**
	 * 新增历史订单
	 */
	@Operation(summary = "新增历史订单")
	@PreAuthorize("@ss.hasPermi('wxz:history:add')")
	@Log(title = "历史订单", businessType = BusinessType.INSERT)
	@PostMapping
	public AjaxResult add(@RequestBody WxzOrderHistory wxzOrderHistory) {
		return toAjax(orderHistoryService.save(wxzOrderHistory));
	}

	/**
	 * 修改历史订单
	 */
	@Operation(summary = "修改历史订单")
	@PreAuthorize("@ss.hasPermi('wxz:history:edit')")
	@Log(title = "历史订单", businessType = BusinessType.UPDATE)
	@PutMapping
	public AjaxResult edit(@RequestBody WxzOrderHistory wxzOrderHistory) {
		return toAjax(orderHistoryService.updateById(wxzOrderHistory));
	}

	/**
	 * 删除历史订单
	 */
	@Operation(summary = "删除历史订单")
	@PreAuthorize("@ss.hasPermi('wxz:history:remove')")
	@Log(title = "历史订单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
	public AjaxResult remove(@PathVariable Long[] ids) {
		return toAjax(orderHistoryService.removeByIds(Arrays.asList(ids)));
	}

	/**
	 * 订单归档到历史
	 */
	@Operation(summary = "订单归档")
	@PreAuthorize("@ss.hasRole('club_leader')")
	@Log(title = "历史订单", businessType = BusinessType.UPDATE)
	@GetMapping("/archive")
	public AjaxResult archiveOrder() {
		log.info("正在进行归档操作...");
		return AjaxResult.success(orderHistoryService.archiveAllOrders());
	}
}
